/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.datamining.gui;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JTextField;

/**
 *
 * @author Sami
 */
public class GUITextBox extends JTextField {

    private String text = "";
    private String fieldName = "";

    public GUITextBox() {
        super();

        addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                guiTextBoxKeyReleased(e);
            }
        });

        addFocusListener(new FocusListener() {

            public void focusGained(FocusEvent e) {
                guiTextBoxFocusGained(e);
            }

            public void focusLost(FocusEvent e) {
                guiTextBoxFocusLost(e);
            }
        });
    }

    @Override
    public String getText() {
        if (text.equals("")) {
            return "";
        }

        return super.getText();
    }

    public void setFieldName(String fName) {
        fieldName = fName;
    }

    private void guiTextBoxKeyReleased(KeyEvent e) {
        text = super.getText();
    }

    private void guiTextBoxFocusGained(FocusEvent e) {
        if (text.equals("")) {
            setForeground(new Color(0, 0, 0));
            setText("");
        }
    }

    private void guiTextBoxFocusLost(FocusEvent e) {
        if (text.equals("")) {
            setForeground(new Color(204, 204, 204));
            setText("Enter " + fieldName);
        }
    }
}
